package com.yiyi.credit.manage.sso.manager.controller;


import com.yiyi.credit.manage.sso.common.Constant;
import com.yiyi.credit.manage.sso.common.exception.ResponseCode;
import com.yiyi.credit.manage.sso.common.page.Pagination;
import com.yiyi.credit.manage.sso.model.dto.response.Response;
import com.yiyi.credit.manage.sso.model.entity.SysInfo;
import com.yiyi.credit.manage.sso.model.entity.SysResource;
import com.yiyi.credit.manage.sso.service.inner.SysInfoService;
import com.yiyi.credit.manage.sso.service.inner.SysResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
public class SysInfoController {

    //private static Logger logger = LoggerFactory.getLogger(SysInfoController.class);

    @Autowired
    SysInfoService sysInfoService;

    @Autowired
    SysResourceService sysResourceService;


    @RequestMapping(value = "/getSysInfoList")
    public Object getSysInfoList() throws Exception {
        List<SysInfo> sysInfoList = sysInfoService.getAllSysInfo();
        return new Response<>(sysInfoList);
    }


    @RequestMapping(value = "/getSysInfoPageList")
    public Object getSysInfoPageList(Integer pageIndex, Integer pageSize) throws Exception {
        Pagination pagination = new Pagination(pageIndex, pageSize);
        Pagination result = sysInfoService.getSysInfoPage(pagination);
        return new Response<>(result);
    }


    @RequestMapping(value = "/getSysInfoById")
    public Object getSysInfoById(String sysInfoId) throws Exception {
        if (StringUtils.isEmpty(sysInfoId)) {
            return new Response<>(ResponseCode.NO_SYS_INFO);
        }
        SysInfo sysInfo = sysInfoService.getSysInfoById(Long.parseLong(sysInfoId));
        return new Response<>(sysInfo);
    }


    @RequestMapping(value = "/addSysInfo")
    public Object addSysInfo(@RequestBody SysInfo sysInfo) throws Exception {

        if (sysInfo == null) {
            return new Response<>(ResponseCode.NO_SYS_INFO);
        }

        //增加系统表
        sysInfoService.registerSysInfo(sysInfo);
        sysInfo = sysInfoService.getSysInfoByName(sysInfo.getName()).get(Constant.INT_ZERO);

        //增加系统资源表
        SysResource sysResource = new SysResource();
        sysResource.setSysInfoId(sysInfo.getId());
        sysResource.setParentId(Constant.ROOT_PID_CODE);
        sysResource.setName(sysInfo.getName());
        sysResourceService.addSysResource(sysResource);
        return new Response<>(ResponseCode.SUCCESS);
    }


    @RequestMapping(value = "/updateSysInfo")
    public Object updateSysInfo(@RequestBody SysInfo sysInfo) throws Exception {

        if (sysInfo == null) {
            return new Response<>(ResponseCode.NO_SYS_INFO);
        }

        sysInfoService.updateSysInfo(sysInfo);
        return new Response<>(ResponseCode.SUCCESS);
    }


    @RequestMapping(value = "/deleteSystemInfo")
    public Object deleteSystemInfo(@RequestBody SysInfo sysInfo) throws Exception {

        if (sysInfo == null) {
            return new Response<>(ResponseCode.NO_SYS_INFO);
        }

        List<SysResource> sysResourcesList = sysResourceService.getSysResourceBySysId(sysInfo.getId());
        if (sysResourcesList != null && sysResourcesList.size() > Constant.INT_ONE) {
            return new Response<>(ResponseCode.SYS_INFO_EXIST_RESOURCE);
        }

        sysInfoService.deleteSysInfo(sysInfo);
        sysResourceService.deleteSysResourceBySid(sysInfo.getId());
        return new Response<>(ResponseCode.SUCCESS);
    }
}
